$(function(){
    /*************************************/
    initPage();
    function initPage() {
        getCollage( 1, initUserInfo);
    }

    $('#hrefShengBao').on('click',function () {
        if(getCookie()){
            window.location.href = './shenBao-index.html'
        }
    });

    // 学院
    $('#stuinstitute').on('change',function () {
        let val = $(this).val();
        getSpecialty( val, 0 );
    });
    // 专业
    $('#profession').on('change',function () {
        let val = $(this).val();
        getClass( val, 0 );
    });


    $('#update-changeInfo-btn').on('click',function () {
        let Rx_userName = /^[\u4e00-\u9fa5]{2,4}$/; //姓名
        let Rx_phone = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;// 手机
        let Rx_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; // 邮箱

        let name = Rx_userName.test( $('#stuname').val() );
        let content = Rx_phone.test( $('#stucontent').val() );
        let email = Rx_email.test( $('#stuemail').val() );
        if( name && content && email ){
            $.ajax({
                url : '/user/update_information.do',
                type: 'post',
                data : $('#form').serialize(),
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data) {
                    if(data.status == 0){
                        spop_ok(data.msg,function () {
                            setCookie(data.data);
                            initPage()
                        },function () {
                        });
                    }else if(data.status == 10){
                        spop_waring(data.msg, function () {}, function () {
                            outLoing();
                        } );
                    }else{
                        spop_er(data.msg);
                    }
                }
            })
        }else{
            spop_waring('输入错误');
        }

    });
    $('#changePW').on('click',function () {
        $.ajax({
            url : '/user/reset_password.do',
            type: 'post',
            data : $('.update-psword').serialize(),
            xhrFields : {
                withCredentials: true
            },
            crossDomain: true,
            success : function (data) {
                if(data.status == 0){
                    spop_ok(data.msg,function () {},function () {
                        outLoing();
                    });
                }else if(data.status == 10){
                    spop_waring(data.msg, function () {}, function () {
                        outLoing();
                    });
                }else if(data.status == 1){
                    spop_er(data.msg,function () {
                        $('.update-psword')[0].reset();
                    });
                }
            }
        })
    });
    // 获取所有学院信息
    function getCollage( OFF, callBack ) {
        let Pr_collage = new Promise( (resolve,reject)=>{
            $.ajax({
                url : '/institute/list.do',
                type : 'post',
                data : {},
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if( data.status == 0 ){
                        resolve(data);
                    }else{
                        reject(data);
                    }
                }
            });// 获取所有学院信息
        } );
        Pr_collage.then( (data)=>{
            let Box = $('#stuinstitute');
            Box.html('');

            let aCollageInfo = data.data;
            for(let i=0; i<aCollageInfo.length; i++){
                $('<option value="'+ aCollageInfo[i].stuinstitutename +'">'+ aCollageInfo[i].stuinstitutename +'</option>').appendTo(Box);
            }
            if( OFF ){
                getSpecialty( userInfo.stuinstitute, 1, callBack );
            }else{
                getSpecialty( aCollageInfo[0].stuinstitutename, 0, callBack );
            }
        }).catch( (data)=>{
            if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing )
            }else{
                spop_waring(data.msg);
            }
        } );
    }
    // 获取专业
    function getSpecialty( str, OFF, callBack) {
        let Pr_specialty = new Promise( (resolve,reject) => {
            $.ajax({
                url : '/profession/list.do',
                type : 'post',
                data : {
                    stuInstitutename : str
                },
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if( data.status == 0 ){
                        resolve(data);
                    }else {
                        reject(data);
                    }
                }
            });
        } );
        Pr_specialty.then( (data)=>{
            let Box = $('#profession');
            Box.html('');

            let aSpecialty = data.data;
            for(let i=0; i<aSpecialty.length; i++ ){
                $('<option value="'+ aSpecialty[i].professionname +'">'+ aSpecialty[i].professionname +'</option>').appendTo(Box);
            }
            if( OFF ){
                getClass( userInfo.profession, callBack );
            }else{
                getClass( aSpecialty[0].professionname, callBack );
            }
        } ).catch( (data)=>{
            if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing )
            }
        } )
    }
    // 获取所有班级
    function getClass( str, callBack) {
        let Pr_class = new Promise( (resolve,reject) => {
            $.ajax({
                url : '/class/list.do',
                type : 'post',
                data : {
                    professionName: str
                },
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if( data.status == 0 ){
                        resolve(data);
                    }else{
                        reject(data);
                    }
                }
            });
        } );// 获取所有班级信息
        Pr_class.then((data)=>{
            let Box = $('#stuclass');
            Box.html('');

            let aClassInfo = data.data;
            for(let i=0; i<aClassInfo.length; i++ ){
                $('<option value="'+ aClassInfo[i].classname +'">'+ aClassInfo[i].classname +'</option>').appendTo( Box );
            }

            if( typeof callBack == 'function'){
                callBack();
            }else{
                $('#stuclass option[value="' + data.classname + '"]').attr('selected','selected');// 班级
            }
        }).catch( (data)=>{
            if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing )
            }else{
                spop_er(data.msg);
            }
        } );
    }
    // 初始化用户信息
    function initUserInfo() {
        /** 初始化用户数据 ****************************************/
        let userInfo = getCookie();
        if( userInfo != null ){
            To_Form(userInfo);// 将用户信息渲染至表单
        }else{
            spop_waring( '未登录', function () {}, outLoing );
        }
    }
    // 将用户信息渲染至表单
    function To_Form(data){
        $('#stuId').html(data.stuid);
        $('#stuname').val(data.stuname);
        $('#stucontent').val(data.stucontent);
        $('#stuemail').val(data.stuemail);
        $('#stubirth').val(data.stubirth);
        $('#stuinstitute option[value="' + data.stuinstitute + '"]').attr('selected','selected');// 学院
        $('#profession option[value="' + data.profession + '"]').attr('selected','selected');// 专业
        $('#stuclass option[value="' + data.stuclass + '"]').attr('selected','selected');// 班级

        // 性别切换
        let sex_index = null;
        if( data.stusex === '男' ){
            sex_index = 0 ;
        }else{
            sex_index = 1 ;
        }

        $('.update-sex').children('label').eq(sex_index).siblings().removeClass('btn-primary').addClass('btn-default');
        $('.update-sex').children('label').eq(sex_index).siblings().children().prop('checked',false);

        $('.update-sex').children('label').eq(sex_index).addClass('btn-primary');
        $('.update-sex').children('label').eq(sex_index).children().prop('checked',true);

        navBarInfo(data);
    }

});

